package operate;

import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        Scanner in = new Scanner(System.in);
        System.out.print("输入要删除的书名：");
        String name = in.nextLine();

        //判断图书是否存在
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if (name.equals(bookList.getBook(i).getName())) {
                //将后面的往前移动覆盖
                for (int j = i; j < bookList.getUsedSize() - 1; j++) {
                    bookList.setBook(j, bookList.getBook(j + 1));
                }

                //修改UsedSize
                bookList.setUsedSize(bookList.getUsedSize() - 1);
                //将最后一个置为null
                bookList.setBook(bookList.getUsedSize(), null);

                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("没有"+name+"这本书！");
    }
}